---
title: 自定义连接按钮
description: 创建自定义连接按钮
---

# 自定义连接按钮

## 创建自定义连接按钮

您可以使用低级别的 `ConnectButton.Custom` 来创建您自己的自定义连接按钮。此组件会渲染一个函数，其中包含重新实现内置按钮所需的一切。

内置按钮的简化重实现可以像这样：

```tsx
import { ConnectButton } from '@rainbow-me/rainbowkit';

export const YourApp = () => {
  return (
    <ConnectButton.Custom>
      {({
        account,
        chain,
        openAccountModal,
        openChainModal,
        openConnectModal,
        authenticationStatus,
        mounted,
      }) => {
        // Note: If your app doesn't use authentication, you
        // can remove all 'authenticationStatus' checks
        const ready = mounted && authenticationStatus !== 'loading';
        const connected =
          ready &&
          account &&
          chain &&
          (!authenticationStatus ||
            authenticationStatus === 'authenticated');

        return (
          <div
            {...(!ready && {
              'aria-hidden': true,
              'style': {
                opacity: 0,
                pointerEvents: 'none',
                userSelect: 'none',
              },
            })}
          >
            {(() => {
              if (!connected) {
                return (
                  <button onClick={openConnectModal} type="button">
                    Connect Wallet
                  </button>
                );
              }

              if (chain.unsupported) {
                return (
                  <button onClick={openChainModal} type="button">
                    Wrong network
                  </button>
                );
              }

              return (
                <div style={{ display: 'flex', gap: 12 }}>
                  <button
                    onClick={openChainModal}
                    style={{ display: 'flex', alignItems: 'center' }}
                    type="button"
                  >
                    {chain.hasIcon && (
                      <div
                        style={{
                          background: chain.iconBackground,
                          width: 12,
                          height: 12,
                          borderRadius: 999,
                          overflow: 'hidden',
                          marginRight: 4,
                        }}
                      >
                        {chain.iconUrl && (
                          <img
                            alt={chain.name ?? 'Chain icon'}
                            src={chain.iconUrl}
                            style={{ width: 12, height: 12 }}
                          />
                        )}
                      </div>
                    )}
                    {chain.name}
                  </button>

                  <button onClick={openAccountModal} type="button">
                    {account.displayName}
                    {account.displayBalance
                      ? ` (${account.displayBalance})`
                      : ''}
                  </button>
                </div>
              );
            })()}
          </div>
        );
      }}
    </ConnectButton.Custom>
  );
};
```

以下 `props` 传递给您的渲染函数。

### 账户属性

<PropsTable
  data={[
    {
      name: 'account',
      required: false,
      type: 'object | undefined',
      description:
        '包含当前账户详细信息的对象，如下所述',
    },
    {
      name: 'account.address',
      required: false,
      type: 'string',
      description: (
        <>
          完整的账户地址，例如 "0x7a3d05c70581bD345fe117c06e45f9669205384f"
        </>
      ),
    },
    {
      name: 'account.balanceDecimals',
      required: false,
      type: 'string | undefined	',
      description: '账户余额（以小数表示）',
    },
    {
      name: 'account.balanceFormatted',
      required: false,
      type: 'string | undefined	',
      description: (
        <>
          账户余额格式化为字符串，例如 {' '}
          <code>1.234567890123456789</code>
        </>
      ),
    },
    {
      name: 'account.balanceSymbol',
      required: false,
      type: 'string | undefined	',
      description: (
        <>
          账户余额的符号，例如 <code>ETH</code>
        </>
      ),
    },
    {
      name: 'account.displayBalance',
      required: false,
      type: 'string | undefined	',
      description: (
        <>
          将余额格式化为3位有效数字，并带有符号，例如{' '}<code>1.23 ETH</code>
        </>
      ),
    },
    {
      name: 'account.displayName',
      required: false,
      type: 'string',
      description: (
        <>
          ENS 名称或地址的截断版本，例如{' '}<code>"rainbowwallet.eth"</code> 或 <code>"0x7a…384f"</code>
        </>
      ),
    },
    {
      name: 'account.ensAvatar',
      required: false,
      type: 'string	| undefined',
      description: 'ENS 头像的 URI',
    },
    {
      name: 'account.ensName',
      required: false,
      type: 'string	| undefined',
      description: (
        <>
          ENS名称，例如{' '}<code>rainbowwallet.eth</code>
        </>
      ),
    },
    {
      name: 'account.hasPendingTransactions',
      required: false,
      type: 'boolean',
      description:
        '布尔值，表示当前链上该账户是否有待处理的交易',
    },
  ]}
/>

### 链属性

<PropsTable
  data={[
    {
      name: 'chain',
      required: false,
      type: 'object | undefined',
      description:
        '包含当前链的详细信息的对象，如下所述',
    },
    {
      name: 'chain.hasIcon',
      required: false,
      type: 'boolean',
      description: '是否为链指定了图标',
    },
    {
      name: 'chain.iconUrl',
      required: false,
      type: 'string | undefined',
      description:
        '链图标 URL（在下载 Base64 数据 URL 时可能也是未定义的）',
    },
    {
      name: 'chain.iconBackground',
      required: false,
      type: 'string | undefined',
      description:
        '图片加载时会显示的链图标背景',
    },
    {
      name: 'chain.id',
      required: false,
      type: 'number',
      description: (
        <>
          链 ID,例如 <code>1</code>
        </>
      ),
    },
    {
      name: 'chain.name',
      required: false,
      type: 'string | undefined',
      description: (
        <>
          链名称，例如 <code>"Ethereum"</code>
        </>
      ),
    },
    {
      name: 'chain.unsupported',
      required: false,
      type: 'boolean | undefined',
      description:
        '一个布尔值，表示当前链是否被支持。',
    },
  ]}
/>

### 模态状态属性

<PropsTable
  data={[
    {
      name: 'openAccountModal',
      required: false,
      type: '() => void',
      description: '打开账户模态窗口的函数',
    },
    {
      name: 'openChainModal',
      required: false,
      type: '() => void',
      description: '打开链模态对话框的函数',
    },
    {
      name: 'openConnectModal',
      required: false,
      type: '() => void',
      description: '打开连接钱包模态框的函数',
    },
    {
      name: 'accountModalOpen',
      required: false,
      type: 'boolean',
      description:
        '指示账户模态窗口是否打开的布尔值',
    },
    {
      name: 'chainModalOpen',
      required: false,
      type: 'boolean',
      description: '一个布尔值，指示链式模态框是否打开',
    },
    {
      name: 'connectModalOpen',
      required: false,
      type: 'boolean',
      description:
        '表示连接模态框是否打开的布尔值',
    },
  ]}
/>

### 常规状态属性

<PropsTable
  data={[
    {
      name: 'mounted',
      required: false,
      type: 'boolean',
      description:
        '布尔值，指示组件是否已挂载',
    },
    {
      name: 'authenticationStatus',
      required: false,
      type: '"loading" | "unauthenticated" | "authenticated" | undefined',
      description:
        '身份验证状态，如果尚未配置身份验证，则为 undefined',
    },
  ]}
/>

